<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
<meta name="baidu-site-verification" content="code-hkfwkpUecU" />
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="true">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto+Slab:300,300italic,400,400italic,700,700italic|PT+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"github.com","root":"/","scheme":"Gemini","version":"8.0.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>

  <meta name="description" content="input() 该函数用来获取用户的输入  input()调用后，程序会立即暂停，等待用户输入，用户输入完内容以后，点击回车程序才会继续向下执行  用户输入完成以后，其所输入的的内容会以返回值得形式返回  注意：input()的返回值是一个字符串  input()函数中可以设置一个字符串作为参数，这个字符串将会作为提示文字显示  input()也可以用于暂时阻止程序结束  123username">
<meta property="og:type" content="article">
<meta property="og:title" content="输入输出">
<meta property="og:url" content="https://github.com/shanlu123/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/index.html">
<meta property="og:site_name" content="shanlu的博客">
<meta property="og:description" content="input() 该函数用来获取用户的输入  input()调用后，程序会立即暂停，等待用户输入，用户输入完内容以后，点击回车程序才会继续向下执行  用户输入完成以后，其所输入的的内容会以返回值得形式返回  注意：input()的返回值是一个字符串  input()函数中可以设置一个字符串作为参数，这个字符串将会作为提示文字显示  input()也可以用于暂时阻止程序结束  123username">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-09-27T16:00:00.000Z">
<meta property="article:modified_time" content="2020-10-04T09:10:09.296Z">
<meta property="article:author" content="shanlu">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://github.com/shanlu123/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>输入输出 | shanlu的博客</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>
  <a href="https://github.com/shanlu123" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">shanlu的博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">三餐四季，冷暖自知</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/home" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-前端">

    <a href="/%E5%89%8D%E7%AB%AF/" rel="section"><i class="fa fa-cloud fa-fw"></i>前端</a>

  </li>
        <li class="menu-item menu-item-生活">

    <a href="/%E7%94%9F%E6%B4%BB/" rel="section"><i class="fa fa-user fa-fw"></i>生活</a>

  </li>
        <li class="menu-item menu-item-school">

    <a href="/school/" rel="section"><i class="fa fa-graduation-cap fa-fw"></i>学校</a>

  </li>
        <li class="menu-item menu-item-tree_hole">

    <a href="/tree_hole/" rel="section"><i class="fa fa-tree fa-fw"></i>树洞</a>

  </li>
        <li class="menu-item menu-item-photos">

    <a href="/photos/" rel="section"><i class="fa fa-camera fa-fw"></i>相册</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#input"><span class="nav-number">1.</span> <span class="nav-text">input()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%93%E5%87%BA%E6%A0%BC%E5%BC%8F"><span class="nav-number">2.</span> <span class="nav-text">输出格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%BB%E5%86%99%E6%96%87%E4%BB%B6"><span class="nav-number">3.</span> <span class="nav-text">读写文件</span></a></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="shanlu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">shanlu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/shanlu123" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;shanlu123"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.cnblogs.com/shanlu0000/p/11478994.html" title="博客园 → https:&#x2F;&#x2F;www.cnblogs.com&#x2F;shanlu0000&#x2F;p&#x2F;11478994.html" rel="noopener" target="_blank"><i class="fa fa-blog fa-fw"></i>博客园</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:yourname@gmail.com" title="E-Mail → mailto:yourname@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/6662419816/home?wvr=5" title="微博 → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;6662419816&#x2F;home?wvr&#x3D;5" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>微博</a>
      </span>
  </div>


  <div class="links-of-blogroll animated">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.npmjs.com/" title="https:&#x2F;&#x2F;www.npmjs.com&#x2F;" rel="noopener" target="_blank">npm</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.bilibili.com/" title="https:&#x2F;&#x2F;www.bilibili.com&#x2F;" rel="noopener" target="_blank">bilibili</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stackoverflow.com/" title="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="noopener" target="_blank">stackoverflow</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://developers.weixin.qq.com/miniprogram/dev/component/" title="https:&#x2F;&#x2F;developers.weixin.qq.com&#x2F;miniprogram&#x2F;dev&#x2F;component&#x2F;" rel="noopener" target="_blank">小程序</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://www.ruanyifeng.com/blog/" title="http:&#x2F;&#x2F;www.ruanyifeng.com&#x2F;blog&#x2F;" rel="noopener" target="_blank">阮一峰</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://cn.aliyun.com/" title="https:&#x2F;&#x2F;cn.aliyun.com&#x2F;" rel="noopener" target="_blank">阿里云</a>
        </li>
    </ul>
  </div>

      </section>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/shanlu123/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="shanlu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shanlu的博客">
    </span>

    
    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          输入输出
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-28 00:00:00" itemprop="dateCreated datePublished" datetime="2020-09-28T00:00:00+08:00">2020-09-28</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-10-04 17:10:09" itemprop="dateModified" datetime="2020-10-04T17:10:09+08:00">2020-10-04</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
        </span>
    </span>

  
    <span id="/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/" class="post-meta-item leancloud_visitors" data-flag-title="输入输出" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6.2k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>6 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="input"><a href="#input" class="headerlink" title="input()"></a>input()</h3><ol>
<li><p>该函数用来获取用户的输入</p>
</li>
<li><p>input()调用后，程序会立即暂停，等待用户输入，用户输入完内容以后，点击回车程序才会继续向下执行</p>
</li>
<li><p>用户输入完成以后，其所输入的的内容会以返回值得形式返回</p>
</li>
<li><p>注意：input()的返回值是一个字符串</p>
</li>
<li><p>input()函数中可以设置一个字符串作为参数，这个字符串将会作为提示文字显示</p>
</li>
<li><p>input()也可以用于暂时阻止程序结束</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">username = input(<span class="string">&#x27;请输入用户名:&#x27;</span>)</span><br><span class="line"><span class="keyword">if</span> username == <span class="string">&#x27;admin&#x27;</span>:</span><br><span class="line">    print(<span class="string">&#x27;管理员登录！&#x27;</span>)</span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ol>
<h3 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h3><ol>
<li><p>几种格式化输出的方法</p>
<ul>
<li><p>格式化字符串</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 字符串相加，拼接（注意，字符串只能和字符串拼接，不能和其他类型做加法连接）</span></span><br><span class="line">a = <span class="number">123</span> </span><br><span class="line">print(<span class="string">&quot;a = &quot;</span>+a) <span class="comment"># 不常用</span></span><br><span class="line">print(<span class="string">&#x27;a =&#x27;</span>,a)  <span class="comment"># 常用</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># a = 123</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建字符串时，可以在字符串中指定占位符</span></span><br><span class="line"><span class="comment"># %s 在字符串中表示任意字符</span></span><br><span class="line"><span class="comment"># %3s，3位字符，不够补空格</span></span><br><span class="line"><span class="comment"># %3.5s，3位到5位字符</span></span><br><span class="line"><span class="comment"># %f 浮点数占位符</span></span><br><span class="line"><span class="comment"># %.2f 两位小数</span></span><br><span class="line"><span class="comment"># %d 整数占位符</span></span><br><span class="line"></span><br><span class="line">a = <span class="string">&#x27;Hello %s&#x27;</span> % <span class="string">&#x27;张三&#x27;</span>  <span class="comment"># a = Hello 张三</span></span><br><span class="line">b = <span class="string">&#x27;hello %s 你好 %s&#x27;</span> % (<span class="string">&#x27;Tom&#x27;</span>, <span class="string">&#x27;张三&#x27;</span>)  <span class="comment"># b = hello Tom 你好 张三</span></span><br><span class="line">c = <span class="string">&#x27;hello %3.5s&#x27;</span> % <span class="string">&#x27;abcdefg&#x27;</span>  <span class="comment"># c = hello abcde</span></span><br><span class="line">d = <span class="string">&#x27;hello %s&#x27;</span> % <span class="number">123.456</span>  <span class="comment"># d = hello 123.456</span></span><br><span class="line">e = <span class="string">&#x27;hello %.2f&#x27;</span> % <span class="number">123.456</span>  <span class="comment"># e = hello 123.46</span></span><br><span class="line">f = <span class="string">&#x27;hello %d&#x27;</span> % <span class="number">123.95</span>  <span class="comment"># f = hello 123</span></span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;a = %s&#x27;</span> % a)</span><br><span class="line">print(<span class="string">&#x27;b = %s&#x27;</span> % b)</span><br><span class="line">print(<span class="string">&#x27;c = %s&#x27;</span> % c)</span><br><span class="line">print(<span class="string">&#x27;d = %s&#x27;</span> % d)</span><br><span class="line">print(<span class="string">&#x27;e = %s&#x27;</span> % e)</span><br><span class="line">print(<span class="string">&#x27;f = %s&#x27;</span> % f)</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在字符串的引号之前加上一个 f 或 F，在字符串中，可以在&#123;&#125; 之间写可以引用的变量或字面值的 python 表达式</span></span><br><span class="line">year = <span class="number">2016</span></span><br><span class="line">str = <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line">print(&#123;year&#125;, &#123;str&#125;)</span><br><span class="line">print(<span class="string">f&#x27;year=<span class="subst">&#123;year&#125;</span>,str=<span class="subst">&#123;str&#125;</span>&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">&#123;2016&#125; &#123;&#x27;hello world&#x27;&#125;</span></span><br><span class="line"><span class="string">year = 2016, str = hello world</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

</blockquote>
</li>
<li><p>字符串的 str.format() 方法</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">42</span>_572_654</span><br><span class="line">b = <span class="number">43</span>_132_495</span><br><span class="line">c = a / (a + b)</span><br><span class="line">print(<span class="string">&#x27;&#123;:-9&#125;,&#123;:2.2%&#125;&#x27;</span>.format(a, c))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">42572654,49.67%</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

<p>仍然使用{} 来标记变量将被替换的位置，并且可以提供详细的格式化指令，但还需要提供要格式化的信息</p>
</blockquote>
</li>
<li><p>当只想快速显示某些变量以进行调试时，可以使用 repr() 或 str() 将任何值转化为字符串</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">s = <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line">print(str(s), repr(s))  <span class="comment"># hello world  &#x27;hello world&#x27;</span></span><br><span class="line">print(str(<span class="number">1</span> / <span class="number">7</span>))  <span class="comment"># 0.14285714285714285</span></span><br><span class="line"></span><br><span class="line">x = <span class="number">10</span> * <span class="number">3.25</span></span><br><span class="line">y = <span class="number">200</span> * <span class="number">200</span></span><br><span class="line">print(repr(x), repr(y))  <span class="comment"># 32.5 40000</span></span><br><span class="line">print(repr((x, y, (<span class="string">&#x27;Tom&#x27;</span>, <span class="string">&#x27;Bob&#x27;</span>))))  <span class="comment"># (32.5, 40000, (&#x27;Tom&#x27;, &#x27;Bob&#x27;))</span></span><br><span class="line"></span><br><span class="line">str = <span class="string">&quot;hello,world\n&quot;</span></span><br><span class="line">print(repr(str))  <span class="comment"># &#x27;hello,world\n&#x27;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
</li>
<li><p>格式化字符串文字</p>
<ul>
<li><p>格式化字符串字面值（f-字符串），可以在字符串前面加上 f 和 F 并将表达式写成 {expression} ，在字符串中包含python表达式的值</p>
</li>
<li><p>可选的格式说明符可以跟在表达式后，可以更好的控制值的格式化方式</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 将PI舍入到小数点后三位</span></span><br><span class="line"><span class="keyword">import</span> math</span><br><span class="line"></span><br><span class="line">print(<span class="string">f&#x27;PI is <span class="subst">&#123;math.pi:<span class="number">.3</span>f&#125;</span>&#x27;</span>)  <span class="comment"># PI is 3.142</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>在 ’ ：’ 后传递一个整数可以让该字段成为最小字符宽度，在列对齐时很有用</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">table = &#123;<span class="string">&#x27;Tom&#x27;</span>: <span class="number">4127</span>, <span class="string">&#x27;Jack&#x27;</span>: <span class="number">4098</span>, <span class="string">&#x27;Alice&#x27;</span>: <span class="number">7678</span>&#125;</span><br><span class="line"><span class="keyword">for</span> name, phone <span class="keyword">in</span> table.items():</span><br><span class="line">    print(<span class="string">f&#x27;<span class="subst">&#123;name:<span class="number">10</span>&#125;</span>==&gt;<span class="subst">&#123;phone:<span class="number">10</span>d&#125;</span>&#x27;</span>)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">Tom       ==&gt;      4127</span></span><br><span class="line"><span class="string">Jack      ==&gt;      4098</span></span><br><span class="line"><span class="string">Alice     ==&gt;      7678</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>其他修饰符可用于在格式化前转化值。’ !a ‘ 应用 ascill() ，’ !s ‘ 应用 str()，’ !r ‘ 应用 repr()</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">animals = <span class="string">&#x27;eels&#x27;</span></span><br><span class="line">print(<span class="string">f&#x27;<span class="subst">&#123;animals&#125;</span>&#x27;</span>)  <span class="comment"># eels</span></span><br><span class="line">print(<span class="string">f&#x27;<span class="subst">&#123;animals!a&#125;</span>&#x27;</span>)  <span class="comment"># &#x27;eels&#x27;</span></span><br><span class="line">print(<span class="string">f&#x27;<span class="subst">&#123;animals!s&#125;</span>&#x27;</span>)  <span class="comment"># eels</span></span><br><span class="line">print(<span class="string">f&#x27;<span class="subst">&#123;animals!r&#125;</span>&#x27;</span>)  <span class="comment"># &#x27;eels&#x27;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
</li>
<li><p>字符串的 format() 方法</p>
<ul>
<li><p>基本用法</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;&#123;&#125;,&quot;&#123;&#125;&quot;&#x27;</span>.format(<span class="string">&#x27;hello&#x27;</span>, <span class="string">&#x27;world&#x27;</span>))  <span class="comment"># hello,&quot;world&quot;</span></span><br></pre></td></tr></table></figure>

</blockquote>
<p>花括号和其中的字符（称为格式字段）将替换为传递给 str.format() 方法的对象。</p>
</li>
<li><p>花括号中的数字可以表示传递给 str.format() 方法的对象的位置</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;&#123;0&#125; and &#123;1&#125;&#x27;</span>.format(<span class="string">&#x27;abc&#x27;</span>, <span class="string">&#x27;ABC&#x27;</span>))  <span class="comment"># abc and ABC</span></span><br><span class="line">print(<span class="string">&#x27;&#123;1&#125; and &#123;0&#125;&#x27;</span>.format(<span class="string">&#x27;abc&#x27;</span>, <span class="string">&#x27;ABC&#x27;</span>))  <span class="comment"># ABC and abc</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>如果在 str.format() 方法中使用关键字参数，则使用参数的名称引用它们的值</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;&#123;name&#125; and &#123;sex&#125;&#x27;</span>.format(name=<span class="string">&#x27;jack&#x27;</span>, sex=<span class="string">&#x27;男&#x27;</span>))  <span class="comment"># jack and 男</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>位置和关键字参数可以任意组合</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;&#123;0&#125;,&#123;1&#125;,and &#123;other&#125;&#x27;</span>.format(<span class="string">&#x27;abc&#x27;</span>, <span class="string">&#x27;ABC&#x27;</span>, other=<span class="string">&#x27;hello&#x27;</span>))  <span class="comment"># abc,ABC,and hello</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>如果是一个很长的格式字符串，不希望把它拆开，那么最好按名称而不是按位置引用变量来进行格式化，这可以通过简单的传递字典并使用方括号访问键来完成</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">table = &#123;<span class="string">&#x27;Tom&#x27;</span>: <span class="number">4127</span>, <span class="string">&#x27;Jack&#x27;</span>: <span class="number">4098</span>, <span class="string">&#x27;Alice&#x27;</span>: <span class="number">7678</span>&#125;</span><br><span class="line">print(<span class="string">&#x27;Tom:&#123;0[Tom]:d&#125;;Jack:&#123;0[Jack]:d&#125;;Alice:&#123;0[Alice]:d&#125;;&#x27;</span>.format(table)) <span class="comment">#Tom:4127;Jack:4098;Alice:7678;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>也可以通过使用 ‘*’ 符号将 table 作为关键字参数传递</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&#x27;Tom:&#123;Tom:d&#125;;Jack:&#123;Jack:d&#125;;Alice:&#123;Alice:d&#125;;&#x27;</span>.format(**table)) <span class="comment">#Tom:4127;Jack:4098;Alice:7678;</span></span><br></pre></td></tr></table></figure>

</blockquote>
</li>
</ul>
<p>这与内置函数 vars() 结合使用时非常有用，会返回包含所有局部变量的字典</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 下面会生成一组整齐的列，其中包含给定的整数和它的平方以及立方</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">4</span>):</span><br><span class="line">    print(<span class="string">&#x27;&#123;0:2d&#125; &#123;1:3d&#125; &#123;2:4d&#125;&#x27;</span>.format(x,x*x,x*x*x))</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string"> 1   1    1</span></span><br><span class="line"><span class="string"> 2   4    8</span></span><br><span class="line"><span class="string"> 3   9   27</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>手动格式化字符串</p>
<ul>
<li><p>平方和立方的表，手动格式化的，每一列之间的一个空格是通过 print() 方式添加的</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">4</span>):</span><br><span class="line">    print(repr(x).rjust(<span class="number">2</span>),repr(x*x).rjust(<span class="number">3</span>),end=<span class="string">&#x27;&#x27;</span>)</span><br><span class="line">    print(repr(x*x*x).rjust(<span class="number">4</span>))</span><br><span class="line">    </span><br><span class="line">   </span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string"> 1   1   1</span></span><br><span class="line"><span class="string"> 2   4   8</span></span><br><span class="line"><span class="string"> 3   9  27</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

</blockquote>
<p>字符串的 str.rjust() 方法通过在左侧填充空格来对给定宽度的字段中的字符串进行右对齐。。类似的还有 str.ljust()  和 str.center() 。</p>
</li>
<li><p>str.zfill() ，它会在数字字符串的左边填充0，能识别正负号。</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="string">&#x27;123&#x27;</span></span><br><span class="line">b = <span class="string">&#x27;-3.14&#x27;</span></span><br><span class="line">print(a.zfill(<span class="number">5</span>))  <span class="comment"># 00123</span></span><br><span class="line">print(b.zfill(<span class="number">7</span>))  <span class="comment"># -003.14</span></span><br><span class="line">print(b.zfill(<span class="number">1</span>))  <span class="comment"># -3.14</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
</li>
<li><p>旧的字符串格式化方法</p>
<ul>
<li><p>% 运算符（求余）也可用于字符串格式化，给定 ‘ string ‘  % values，则 string 中的 % 实例会以0个或多个 values 元素替换。此操作通常被称为字符串插值</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> math</span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;PI is %5.3f&#x27;</span> % math.pi)  <span class="comment"># PI is 3.142</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
</li>
</ol>
<h3 id="读写文件"><a href="#读写文件" class="headerlink" title="读写文件"></a>读写文件</h3><ol>
<li><p>open() 返回一个 file mode，最常用的有两个参数：open(filename，mode)</p>
<ul>
<li><p>第一个参数是包含文件名的字符串，第二个参数是另一个字符串，其中包含一些描述文件使用方式的字符。</p>
</li>
<li><p>mode 可以是 ‘r’，表示文件只读；’w’ 表示文件只能写入（已存在的同名文件会被删除）’a’表示打开文件以追加内容，任何写入的数据会自动添加到文件末尾。 ‘r+’ 表示打开文件进行读写。 mode 参数可选，省略时默认值是”r”</p>
</li>
<li><p>通常文件是以 text mode 打开的，这意味着从文件中读取或写入字符串时，都会以指定的编码方式进行编码。如果未指定编码方式，默认值与平台无关。在 mode 中追加的 ‘b’ 则以 binary mode 打开文件，这个模式应该用于所有不包含文本的文件</p>
</li>
<li><p>在文本模式读取时，默认会把平台特定的行结束符（Unix的\n，Windows的 \r\n）转换为 \n，</p>
</li>
<li><p>在文本模式写入时，默认会把出现的 \n 转化回平台特定的结束符</p>
</li>
<li><p>处理文件对象时，最好使用 with 关键字，当子句体结束后文件会正确关闭。</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> open(<span class="string">&#x27;workfile&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">	read_data = f.read()</span><br><span class="line"></span><br><span class="line">f.closed </span><br><span class="line"><span class="comment"># True</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>若没有使用 with 关键字，那么应该调用 f.close() 来关闭文件并立即释放它使用的所有系统资源。</p>
</li>
</ul>
</li>
<li><p>文件对象的方法</p>
<ul>
<li><p>读取文件内容，f.read(size)，它会读取一些数据并将其作为字符串（在文本模式下）或字节串对象（在二进制模式下）返回。size是一个可选的数值参数，当 size 被省略或者为负数时，将读取并返回整个文件的内容，当取其他值时，将读取并返回至多 size 个字符（文本模式下），或 size 个字节（二进制模式下）。如果已到达文件末尾，f.read() 将返回一个空字符串(‘ ‘)</p>
</li>
<li><p>f.readline() 从文件中读取一行，换行符 \n 留在字符串末尾，如果文件不以换行符结尾，则在文件的最后一行省略。如果 f.readline() 返回一个空的字符串，则表示已经到达文件末尾，而空行使用 ‘ \n ‘ 表示。</p>
</li>
<li><p>要从文件中读取行，可以循环遍历文件对象 </p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">f= open(<span class="string">&#x27;workfile&#x27;</span>,<span class="string">&#x27;w&#x27;</span>)</span><br><span class="line"><span class="keyword">for</span> line <span class="keyword">in</span> f:</span><br><span class="line">	print(line,end=<span class="string">&#x27;&#x27;</span>)</span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>如果希望以列表形式读取文件中的所有行，可以使用 list(f) 或 f.readlines()</p>
</li>
<li><p>f.write(string) 会把 string 的内容写入到文件中，并返回写入的字符数，在写入其他类型的对象之前，需要先把它们转化为字符串（文本模式下）或字节对象（二进制模式下）</p>
</li>
<li><p>f.tell() 返回一个整数，给出文件对象在文件中的当前位置，表示为从二进制模式下时从文件开始的字节数，以及文本模式下的意义不明的数字。</p>
</li>
<li><p>f.seek(offset，whence) ，可以改变文件对象的位置，通过向一个参考点添加 offset 来计算位置；参考点由 whence 参数指定。whence 取0 表示从文件开头起算，1表示使用当前文件位置，2表示使用文件末尾作为参考点。whence 如果省略则默认值是0，即使用文件开头作为参考点。</p>
</li>
</ul>
</li>
<li><p>使用 json 保存结构化数据</p>
<ul>
<li><p>一个对象 x，可以用一行简单的代码来查看它的JOSN字符串表表示</p>
<blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line">a=json.dumps([<span class="number">1</span>, <span class="string">&#x27;simple&#x27;</span>, <span class="string">&#x27;list&#x27;</span>])</span><br><span class="line">print(a)   <span class="comment">#[1, &quot;simple&quot;, &quot;list&quot;]</span></span><br></pre></td></tr></table></figure>
</blockquote>
</li>
<li><p>dumps() 函数的另一个变体是 dump() ，它只是将对象序列化为 text file。</p>
</li>
</ul>
</li>
</ol>

    </div>

    
    
    

    
      <div>
       <div>
  
    <div style="text-align:center;color:#bfbfbf;font-size:16px;">
      <span>-------- 本文结束 </span>
      <i class="fa fa-snowflake-o"></i>
      <span> 感谢阅读 --------</span>
    </div>
  
</div>
      </div>
    

    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button onclick="document.querySelector('.post-reward').classList.toggle('active');">
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="shanlu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          <div class="post-tags">
              <a href="/tags/python/" rel="tag"><i class="fa fa-tag"></i> python</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2020/09/27/python/%E6%A8%A1%E5%9D%97/" rel="prev" title="模块">
                  <i class="fa fa-chevron-left"></i> 模块
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2020/09/29/python/%E7%B1%BB%20/" rel="next" title="类和对象">
                  类和对象 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
  
  
  



      
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">shanlu</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">225k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:25</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>


    <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="//cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  <script src="/js/local-search.js"></script>















  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  
<script>
NexT.utils.loadComments('#valine-comments', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', () => {
    new Valine(Object.assign({
      el  : '#valine-comments',
      path: "/2020/09/28/python/%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA/",
    }, {"enable":true,"appId":"M0TTE46ti90I3BUUJqD5WYyD-gzGzoHsz","appKey":"UelegRsJXosPYDakAbUdO5e8","notify":true,"verify":false,"placeholder":"留下评论吧","avatar":"mm","meta":["nick","mail","link"],"pageSize":10,"lang":"zh-cn","visitor":true,"comment_count":true,"recordIP":false,"serverURLs":null,"enableQQ":false,"requiredFields":[]}
    ));
  }, window.Valine);
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":225,"height":450},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body>
</html>
